using System;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.Health.Samples.PatientConnect.ContosoHealth.HealthVaultAdapter;
using System.IO;

namespace Microsoft.Health.Samples.PatientConnect.ContosoHealth.ContosoHealthScheduler
{
    public class ServiceOutboundCalls
    {

        /// <summary>
        /// This method calls methods from PushHealthVaultItemTypes class.
        /// </summary>
        public static void CallOutboundMethods()
        {
            Patient patient;
            Guid appId = new Guid(SchedulerConfig.ApplicationId.Trim());
            string practiceFolderPath = SchedulerConfig.PracticeFolderPath;
            string ccrFolderPath = Path.Combine(practiceFolderPath, Constants.CcrFolderName);
            string ccdFolderPath = Path.Combine(practiceFolderPath, Constants.CcdFolderName);
            string labResultFolderPath = Path.Combine(practiceFolderPath, Constants.LabResultsFolderName);
            List<Patient> patientList = new List<Patient>();
            patient = new Patient(Path.Combine(practiceFolderPath, "Patients.xml"));

            //Selects all registered patient from database
            patientList = patient.SelectPatientRecords("RegisteredWithHealthVault=true");
            foreach (Patient registeredPatient in patientList)
            {
                PushHealthVaultItemTypes outbound = new PushHealthVaultItemTypes(appId);

                UploadCcrDocument(ccrFolderPath, registeredPatient, outbound);

                UploadCcdDocument(ccdFolderPath, registeredPatient, outbound);

                UploadLabResultDocument(labResultFolderPath, registeredPatient, outbound);
            }
        }
        /// <summary>
        /// Upload lab result document
        /// </summary>
        /// <param name="labResultFolderPath">lab result folder path</param>
        /// <param name="registeredPatient">patient</param>
        /// <param name="outbound">outbound object</param>
        private static void UploadLabResultDocument(string labResultFolderPath,
                                                    Patient registeredPatient,
                                                    PushHealthVaultItemTypes outbound)
        {
            //Upload Lab Result
            DirectoryInfo labResultDirInfor = new DirectoryInfo(labResultFolderPath);
            FileInfo[] labResultFileInfo = labResultDirInfor.GetFiles("*.xml");
            foreach (FileInfo finfo in labResultFileInfo)
            {
                if (finfo.Name.Contains(registeredPatient.PatientId.ToString()))
                {
                    outbound.UploadLabResults(Path.Combine(labResultFolderPath, finfo.Name), registeredPatient.PersonId, registeredPatient.RecordId);
                    File.Move(Path.Combine(labResultFolderPath, finfo.Name),
                        Path.Combine(labResultFolderPath, @"Uploaded\" + finfo.Name));
                }
            }
        }

        /// <summary>
        /// upload ccd document 
        /// </summary>
        /// <param name="ccdFolderPath">ccd folder path</param>
        /// <param name="registeredPatient">patient</param>
        /// <param name="outbound">outbound</param>
        private static void UploadCcdDocument(string ccdFolderPath,
                                              Patient registeredPatient,
                                              PushHealthVaultItemTypes outbound)
        {
            DirectoryInfo CCDdirInfo = new DirectoryInfo(ccdFolderPath);
            FileInfo[] ccdFileInfo = CCDdirInfo.GetFiles("*.xml");
            foreach (FileInfo finfo in ccdFileInfo)
            {
                if (finfo.Name.Contains(registeredPatient.PatientId.ToString()))
                {
                    outbound.UploadCcd(Path.Combine(ccdFolderPath, finfo.Name), registeredPatient.PersonId, registeredPatient.RecordId);
                    File.Move(Path.Combine(ccdFolderPath, finfo.Name),
                        Path.Combine(ccdFolderPath, @"Uploaded\" + finfo.Name));
                }
            }
        }

        /// <summary>
        /// upload ccr folder path
        /// </summary>
        /// <param name="ccrFolderPath">ccr folder path</param>
        /// <param name="registeredPatient">patient</param>
        /// <param name="outbound">outbound</param>
        private static void UploadCcrDocument(string ccrFolderPath,
                                              Patient registeredPatient,
                                              PushHealthVaultItemTypes outbound)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(ccrFolderPath);
            FileInfo[] fileInfo = dirInfo.GetFiles("*.xml");

            foreach (FileInfo finfo in fileInfo)
            {
                if (finfo.Name.Contains(registeredPatient.PatientId.ToString()))
                {
                    //Uploads the report to Health Vault
                    outbound.UploadCcr(Path.Combine(ccrFolderPath, finfo.Name), registeredPatient.PersonId, registeredPatient.RecordId);
                    File.Move(Path.Combine(ccrFolderPath, finfo.Name),
                        Path.Combine(ccrFolderPath, @"Uploaded\" + finfo.Name));
                }
            }
        }
    }
}
